package com.meepo.feed.topic

import android.os.Bundle
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.meepo.basic.components.ImageLoaderFactory
import com.meepo.basic.components.listfragment.MeepoListFragment
import com.meepo.basic.components.listfragment.ListFragmentParams
import com.meepo.basic.router.RouterManager
import com.meepo.basic.schema.common.PageResult
import com.meepo.basic.schema.common.ResponseWrapper
import com.meepo.framework.design.button.SlickButton
import com.meepo.framework.design.desc.Data
import com.meepo.framework.design.desc.DescLayout
import com.meepo.feed.R
import com.meepo.feed.API
import com.meepo.basic.schema.feed.Topic
import com.meepo.framework.design.recycler.ViewHolder
import com.meepo.sdk.utils.ActivityHelper
import com.xiaojinzi.component.anno.FragmentAnno
import com.xiaojinzi.component.support.ParameterSupport
import io.reactivex.Observable

@FragmentAnno(RouterManager.FeedModule.TopicListFragment_Path)
class TopicListFragment : MeepoListFragment<Topic>() {

    companion object {
        fun instance(
            keyword: String = "",
            authorId: String = "",
            simple: Boolean = false,
            joinedByUserId: String = "",
            listFragmentParams: ListFragmentParams = ListFragmentParams()

        ): TopicListFragment {
            val rsp = TopicListFragment()
            val bundle = Bundle()
            bundle.putString("keyword", keyword)
            bundle.putString("authorId", authorId)
            bundle.putBoolean("simple", simple)
            bundle.putString("joinedByUserId", joinedByUserId)
            bundle.putSerializable("listFragmentParams", listFragmentParams)
            rsp.arguments = bundle
            return rsp
        }
    }

    override fun loadDataObservable(page: Int): Observable<ResponseWrapper<PageResult<Topic>>> {
        val keyword = ParameterSupport.getString(arguments, "keyword") ?: ""
        val authorId = ParameterSupport.getString(arguments, "authorId") ?: ""
        val joinedByUserId = ParameterSupport.getString(arguments, "joinedByUserId") ?: ""

        return API.instance().fetchTopics(keyword = keyword, authorId = authorId, joined = joinedByUserId, page = page)
    }


    override fun bind(item: Topic, holder: ViewHolder, position: Int) {

        holder.itemView.setOnClickListener {
            ActivityHelper.launch(requireActivity(), TopicDetailActivity::class.java, "topicId", item.id)
        }

        holder.getView<DescLayout>(R.id.desc_view)?.setData(Data(avatar = item.icon, title = item.title))

        // 加入按钮
        holder.getView<SlickButton>(R.id.extra_btn)?.let {

            val unjoinedColor = this.resources.getColor(R.color.primary_color)
            val joinedColor = this.resources.getColor(R.color.primary_text_color)

            if (item.isJoined()) {
                it.text = "已加入"
                it.color = joinedColor
                it.setTextColor(joinedColor)
            } else {
                it.text = "加入"
                it.color = unjoinedColor
                it.setTextColor(unjoinedColor)
            }

            it.setOnClickListener {
                if (item.isJoined()) {
                    XPopup.Builder(context)
                        .isDestroyOnDismiss(true)
                        .asConfirm(
                            "", """确定要退出话题 #${item.title} 吗?""".trimIndent(),
                            "再想想", "退出",
                            OnConfirmListener {

                                presenter.call(
                                    API.instance().deleteTopicRelation(item.id),
                                    {
                                        this.replace(position, item.setQuitted())
                                    }
                                )

                            }, null, false, R.layout.topic_list_item_confirm_popup
                        )
                        .show()

                } else {

                    presenter.call(
                        API.instance().addTopicRelation(item.id),
                        {
                            this.replace(position, item.setJoined())
                        }
                    )
                }
            }

        }

        // 成员数量
        holder.setText(R.id.joined_count_tv, "" + (item.stat?.memberCount ?: 0))

        // 描述
        holder.setText(R.id.topic_desc_tv, item.desc)

        // 封面
        val imageLoader = ImageLoaderFactory.getImageLoader(requireContext(), 500)
        item.covers?.let {
            if (it.isNotEmpty()) holder.setImagePath(R.id.cover_siv1, it[0].content, imageLoader)
            if (it.size > 1) holder.setImagePath(R.id.cover_siv2, it[1].content, imageLoader)
            if (it.size > 2) holder.setImagePath(R.id.cover_siv3, it[2].content, imageLoader)
            if (it.size > 3) holder.setImagePath(R.id.cover_siv4, it[3].content, imageLoader)
        }
    }

    override fun getItemViewLayoutId(item: Topic, position: Int): Int {
        val simple = ParameterSupport.getBoolean(arguments, "simple") ?: false

        return if (simple) R.layout.topic_list_item_simple else R.layout.topic_list_item
    }

    override fun getItemSkeletonViewLayoutId(): Int {
        return R.layout.topic_list_skeleton_item
    }

}